<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--流程退回调用QMS接口--%>
<%

    String requestid = "294294";
    String tablename = "formtable_main_301";
    RecordSet rs = new RecordSet();
    try {
        String currentDate = DateUtil.getCurrentDate();
        rs.executeQuery("select sqr,lzsj,gzjjr,jszysl,lcjdczzzysl,dbsyzysl from " + tablename + " where requestid=?", requestid);
        rs.next();
        String sqr = Util.null2String(rs.getString("sqr"));
        String lzsj = Util.null2String(rs.getString("lzsj"));
        String gzjjr = Util.null2String(rs.getString("gzjjr"));
        int jszysl = Util.getIntValue(rs.getString("jszysl"), 0);
        int lcjdczzzysl = Util.getIntValue(rs.getString("lcjdczzzysl"), 0);
        int dbsyzysl = Util.getIntValue(rs.getString("dbsyzysl"), 0);
        out.print(">>>>>WeaLzlcAction>>>>compDate=" + DateUtil.compDate(lzsj, currentDate)+"<br/>");
        if (DateUtil.compDate(lzsj, currentDate) <= 0) {
            if (!("0".equals(jszysl) && "0".equals(lcjdczzzysl) && "0".equals(dbsyzysl))) {
                JSONObject params = new JSONObject();
                params.put("fromid", sqr);
                params.put("toid", gzjjr);
                params.put("transferType", "resource");
                params.put("authorityTag", "transfer");
                params.put("submitJson", "");
                params.put("T122All", 1);
                params.put("T122AllNum", jszysl);
                params.put("T131All", 1);
                params.put("T131AllNum", lcjdczzzysl);
                params.put("T133All", 1);
                params.put("T133AllNum", dbsyzysl);
                out.print(">>>>>WeaLzlcAction>>>>>params=" + params+"<br/>");
                JSONObject tokenJson = getApplytoken();
                JSONObject toDataJson = tokenJson.getJSONObject("data");
                String token = toDataJson.getString("token");
                JSONObject resultJson = sendPostOKHttpRequest(params,token);
                out.print(">>>>>WeaLzlcAction>>>>>resultJson=" + resultJson+"<br/>");
            }
        }
    } catch (Exception e) {
        out.print(">>>>>WeaCzgdlcAction>>>>>Exception=" + e);
    }
%>

<%!

    private static final String BASE_URL = "http://121.37.152.240:8080";
    private static final String APPID = "1CDCE4BD07A742FDBF8AFB79AF765CEA";
    private static final String SECRET = "MxH/JX/D63EZh1KAzq0311VNMcCld21N8hjiRiaHdB2niKDM8ECoT4NDeL1mi0AcU4wmmeIdax9a5b8EInetKO4YE6uv8LAgeg6W9XQRTWAHuTNr3OePTyJFu+O4vCzFgm35XjJjO+slO11qic+3UB7hLPRvXze4x178GH+g7IwDa9JcQea7DOF+DudgZzTxCKYyf0V4DEDVgoxOT07lJeka7Gwre0V7x7/AOu4UE/GILOCh1UUqCwN2ONTZyUseqq7J2Udy4Y1ckJsUW0kOgcxdqWAODXeDQ2018qXZu05bYLCQ3jdo5oXM+zo4fJtw1kU8jCRTNEFyODEzmc2sJg==";
    private static final String USERID = "KZKbWex39O/DQiaHw4bGqIqkokwM3VJy03QmDXjRO/bjIyfXVAPUP61f054yxCqywIBpLstZx9f7mdfJSF4mFnyQBGXKE+x1n5x6XGpDVf3HZV+VwWT0x7fWhS+Uvp6DokmRxkaQvTUvQNYLnPe4AtRkW7FRmq+xekJWi6IXkZfzAdB6Rdb74bfeGyBKgRDdL6zvlGf6BAoL4WcnCYVHntQP/movHORj5wGK4cj4Ld3SDwgQdrDes+GHWecf0EX2720GNYE1pXUYzqgYYx5aW9wR7B8h/647uuNrlSFEerv0n2OXhJlM+9vq9eanE6XQkZ8t62kRMujSUB4OvGj9TQ==";
    /**
     * 发送请求
     *
     * @param paramsJson
     * @return
     */
    public JSONObject sendPostOKHttpRequest(JSONObject paramsJson,String token) {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        String params = "fromid="+paramsJson.getString("fromid")+"&toid="+paramsJson.getString("toid")+
                "&transferType=resource&authorityTag=transfer&submitJson=" +
                "&T122All=1&T122AllNum=" +paramsJson.getString("T122AllNum")+
                "&T131All=1&T131AllNum=" +paramsJson.getString("T131AllNum")+
                "&T133All=1&T133AllNum="+paramsJson.getString("T133AllNum");
        String apiUrl = BASE_URL+"/api/hrm/permissiontoadjust/processData"+"?"+params;
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "");
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .addHeader("appid",APPID)
                    .addHeader("token",token)
                    .addHeader("userid",USERID)
                    .method("POST", body)
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            new BaseBean().writeLog(">>>>>sendOKHttpRequest>>>>>Exception=" + e.getMessage(), e);
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }
    /**
     * 获取token
     * @return
     */
    public JSONObject getApplytoken() {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        String apiUrl = BASE_URL+"/api/ec/dev/auth/applytoken";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "");
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .addHeader("appid",APPID)
                    .addHeader("secret",SECRET)
                    .method("POST", body)
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            new BaseBean().writeLog(">>>>>sendOKHttpRequest>>>>>Exception=" + e.getMessage(), e);
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }


%>


